package com.karl.loadingviews.headers;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.karl.loading.loadings.IndicatorLoadingView;
import com.karl.loadingviews.BaseHeaderView;
import com.karl.loadingviews.R;
import com.lcodecore.tkrefreshlayout.OnAnimEndListener;

/**
 * Created by Karl on 2017/8/28 0028.
 * desc:
 */

public class IndicatorHeaderView extends BaseHeaderView{

    private IndicatorLoadingView loadingView;

    public IndicatorHeaderView(@NonNull Context context) {
        this(context,null);
    }

    public IndicatorHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.header_indicator,this);
        loadingView = findViewById(R.id.loading_indicator);
        loadingView.setColor(Color.BLUE);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction < 1){
            loadingView.updateProgress(Math.round(fraction * loadingView.getMaxProgress()));
        }else {
            loadingView.updateProgress(loadingView.getMaxProgress());
        }
    }

    @Override
    public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
        if (fraction<1){
            loadingView.updateProgress(fraction * loadingView.getMaxProgress());
        }
    }

    @Override
    public void startAnim(float maxHeadHeight, float headHeight) {
        loadingView.startAnim();
    }

    @Override
    public void onFinish(OnAnimEndListener animEndListener) {
        animEndListener.onAnimEnd();
    }

    @Override
    public void reset() {
        loadingView.stopAnim();
    }
}
